package com.xiao.newfarm.controller;

import com.xiao.newfarm.utils.WxUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author xiaoxiaoping
 * @Date 2020/6/6 17:21
 * @Version 1.0
 */
@RestController
@RequestMapping
public class IndexController {
	
	@Autowired
	private WxUtils wxUtils;
	@GetMapping("index")
	public void check(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String signature=request.getParameter("signature");
		String timestamp=request.getParameter("timestamp");
		String nonce=request.getParameter("nonce");
		String echostr=request.getParameter("echostr");
		PrintWriter pWriter=response.getWriter();
		if(wxUtils.checkSignature(signature, timestamp, nonce)) {
			pWriter.print(echostr);
			pWriter.close();
		}
	}
	@GetMapping("get-token")
	public String getToken() throws Exception {
		return wxUtils.getAccessToken();
	}
}
